﻿using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting;
using System.ComponentModel;
using System;
using SDFL.Remoting;

namespace SDFL.Native
{ 
    /// <summary>
    /// This class is a base class for all Maui classes, all classes derived from it are remotable.
    /// This class serves 2 purposes:
    ///     - To customize MarshalByRefObject behavior (lifetime, etc).
    ///     - To hide unwanted MarshalByRefObject members from being displayed by IntelliSense.   
    /// </summary>
    public class RemotingObject: MarshalByRefObject
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override object InitializeLifetimeService()
        {
            ILease lease = (ILease)base.InitializeLifetimeService();

            if (lease.CurrentState == LeaseState.Initial)
            {
                object sponsor = AppDomain.CurrentDomain.GetData(RemotingConstants.SponsorMarker);
                if (sponsor != null && sponsor is ISponsor)
                    {
                        lease.Register((ISponsor)sponsor);
                    }
            }
            return lease;
        }
    }
}
